// goods/pages/index7/index7.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tempFilePath:'https://img-blog.csdnimg.cn/img_convert/b573b00bed7126db2c209ed01eb35189.png',
    isShowSetting: false
  },

  selectMedia() {
    let _this = this

    // 选择图片
    wx.chooseMedia({
      camera: "front",
      sourceType: ['album'],
      success(res) {
        _this.setData({
          tempFilePath: res.tempFiles[0].tempFilePath
        })

        // 保存到相册，判断用户是否具授权某个权限
       _this.getAuth()
      }
    })

  },


  getAuth(){
    const _this = this
    // 保存到相册，判断用户是否具授权某个权限
    wx.getSetting({
      success(res) {
        // 用户没有此权限
        if (!res.authSetting['scope.writePhotosAlbum']) {
          console.log('用户没有此权限')
          wx.showModal({
            title: '提示',
            showCancel:false,
            content: '检测你没有存相册权限，是否去设置打开',
            complete: (res) => {
            
              if (res.confirm) {
                console.log('confirm')
                // 获取对应的权限
                wx.authorize({
                  scope: 'scope.writePhotosAlbum',
                  success() {
                    console.log('用户允许保存到相册')
                    wx.saveImageToPhotosAlbum({
                      filePath: _this.data.tempFilePath
                    })
                  },
                  fail() {
                  console.log('fail writePhotosAlbum')
                   wx.showModal({
                     title: '权限',
                     content: '打开设置页面',
                     showCancel:false,
                     complete: (res) => {
                       if (res.confirm) {
                         // 打开设置页面
                         wx.openSetting({
                           success(res){
                             // 获取用户是否有没有在设置页面开启
                             if(!res.authSetting['scope.writePhotosAlbum']){
                               _this.getAuth()
                             } else {
                              wx.saveImageToPhotosAlbum({
                                filePath: _this.data.tempFilePath
                              })
                             }
                           }
                         })
                       }
                     }
                   })
                  }
                })
               
              }
            }
          })
        } else {
          console.log('有权限,保存到相册')
          wx.saveImageToPhotosAlbum({
            filePath: _this.data.tempFilePath
          })
        }
      }
    })
  },

  async set() {
    // 同步
    wx.setStorageSync('title', 'hello')
    wx.setStorageSync('user', {
      name: '佩奇',
      age: 12
    })
    wx.setStorageSync('nums', [1, 2, 3, 4, 5])

    // 异步
    let data = await wx.setStorage({
      key: 'fruit',
      data: '西瓜',
    })

    console.log(data)

  },
  async get() {
    const title = wx.getStorageSync('title')
    const user = wx.getStorageSync('user')
    console.log(title)
    console.log(user)

    // 异步获取
    const fruit = await wx.getStorage({
      key: "fruit"
    })
    console.log(fruit.data)

  },
  remove() {
    wx.removeStorageSync('title')
  },

  clear() {
    wx.clearStorageSync()
  },


})